import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import unittest

class TestCoupleInteractions(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Edge()
        self.driver.implicitly_wait(10)
        self.base_url = 'http://127.0.0.1:5000'
        
    def tearDown(self):
        if hasattr(self, 'driver'):
            self.driver.quit()

    def _login_user(self, username_or_email, password):
        """登录用户的辅助方法"""
        self.driver.get(f'{self.base_url}/login')
        time.sleep(3)
        
        username_input = self.driver.find_element(By.ID, 'username_or_email')
        username_input.send_keys(username_or_email)
        time.sleep(1)
        
        password_input = self.driver.find_element(By.ID, 'password')
        password_input.send_keys(password)
        time.sleep(1)
        
        submit_button = self.driver.find_element(By.CLASS_NAME, 'btn')
        submit_button.click()
        time.sleep(1)

    def test_vote_stay_together(self):
        """测试投票 - 继续走下去"""
        self._login_user('admin', '123456')
        self.driver.get(f'{self.base_url}/couple/1')
        time.sleep(1)
        
        stay_button = self.driver.find_element(By.CSS_SELECTOR, 'button[data-vote="stay_together"]')
        stay_button.click()
        time.sleep(1)
        
        # 验证投票计数增加
        stay_count = self.driver.find_element(By.CSS_SELECTOR, '.vote-count.stay-count').text
        self.assertNotEqual(stay_count, '0')

    def test_vote_divorce(self):
        """测试投票 - 和平分手"""
        self._login_user('admin', '123456')
        self.driver.get(f'{self.base_url}/couple/1')
        time.sleep(1)
        
        divorce_button = self.driver.find_element(By.CSS_SELECTOR, 'button[data-vote="divorce"]')
        divorce_button.click()
        time.sleep(1)
        
        # 验证投票计数增加
        divorce_count = self.driver.find_element(By.CSS_SELECTOR, '.vote-count.divorce-count').text
        self.assertNotEqual(divorce_count, '0')

    def test_like_couple(self):
        """测试点赞功能"""
        self._login_user('admin', '123456')
        self.driver.get(f'{self.base_url}/couple/1')
        time.sleep(1)
        
        like_button = self.driver.find_element(By.CSS_SELECTOR, 'button.like-btn')
        like_button.click()
        time.sleep(1)
        
        # 验证点赞计数增加
        like_count = self.driver.find_element(By.CSS_SELECTOR, '.like-count').text
        self.assertNotEqual(like_count, '256')

    def test_comment_on_couple(self):
        """测试评论功能"""
        self._login_user('admin', '123456')
        self.driver.get(f'{self.base_url}/couple/1')
        time.sleep(1)
        
        comment_input = self.driver.find_element(By.ID, 'comment-input')
        comment_input.send_keys('这是一个测试评论')
        time.sleep(1)
        
        submit_button = self.driver.find_element(By.CSS_SELECTOR, '#comment-form button[type="submit"]')
        submit_button.click()
        time.sleep(2)
        
        # 验证评论是否显示
        comments_list = self.driver.find_element(By.CLASS_NAME, 'comments-list').text
        self.assertIn('这是一个测试评论', comments_list)

if __name__ == '__main__':
    unittest.main() 